﻿using Microsoft.VisualBasic;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;

namespace Koala.Pro.CodeGenerator.UI.DatabaseModel.Schema
{
    public class CString
    {
        private string mystring = string.Empty;

        public int Length => mystring.Length;

        public CString()
        {
        }

        public CString(string mystring)
        {
            this.mystring = mystring;
        }

        public override string ToString()
        {
            return mystring.ToString();
        }

        public string ToLower()
        {
            return mystring.ToLower();
        }

        public string ToLowerInvariant()
        {
            return mystring.ToLowerInvariant();
        }

        public string ToUpper()
        {
            return mystring.ToUpper();
        }

        public string ToUpperInvariant()
        {
            return mystring.ToLowerInvariant();
        }

        public string Trim()
        {
            return mystring.Trim();
        }

        public string TrimStart()
        {
            return mystring.TrimStart();
        }

        public string TrimEnd()
        {
            return mystring.TrimEnd();
        }

        public string Replace(char oldChar, char newChar)
        {
            return mystring.Replace(oldChar, newChar);
        }

        public string Replace(string oldValue, string newValue)
        {
            return mystring.Replace(oldValue, newValue);
        }

        public string Remove(int startIndex)
        {
            return mystring.Remove(startIndex);
        }

        public string Remove(int startIndex, int count)
        {
            return mystring.Remove(startIndex, count);
        }

        public override bool Equals(object obj)
        {
            return mystring.Equals(obj);
        }

        public bool Equals(string value)
        {
            return mystring.Equals(value);
        }

        public bool Equals(string value, StringComparison comparisonType)
        {
            return mystring.Equals(value, comparisonType);
        }

        public int CompareTo(object value)
        {
            return mystring.CompareTo(value);
        }

        public int CompareTo(string strB)
        {
            return mystring.CompareTo(strB);
        }

        public bool Contains(string value)
        {
            return mystring.Contains(value);
        }

        public object Clone()
        {
            return mystring.Clone();
        }

        public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
        {
            mystring.CopyTo(sourceIndex, destination, destinationIndex, count);
        }

        public bool EndsWith(string value)
        {
            return mystring.EndsWith(value);
        }

        public bool EndsWith(string value, StringComparison comparisonType)
        {
            return mystring.EndsWith(value, comparisonType);
        }

        public bool EndsWith(string value, bool ignoreCase, CultureInfo culture)
        {
            return mystring.EndsWith(value, ignoreCase, culture);
        }

        public CharEnumerator GetEnumerator()
        {
            return mystring.GetEnumerator();
        }

        public override int GetHashCode()
        {
            return mystring.GetHashCode();
        }

        public TypeCode GetTypeCode()
        {
            return mystring.GetTypeCode();
        }

        public string PadLeft(int totalWidth)
        {
            return mystring.PadLeft(totalWidth);
        }

        public string PadLeft(int totalWidth, char paddingChar)
        {
            return mystring.PadLeft(totalWidth, paddingChar);
        }

        public string PadRight(int totalWidth)
        {
            return mystring.PadRight(totalWidth);
        }

        public string PadRight(int totalWidth, char paddingChar)
        {
            return mystring.PadRight(totalWidth, paddingChar);
        }

        public char[] ToCharArray()
        {
            return mystring.ToCharArray();
        }

        public char[] ToCharArray(int startIndex, int length)
        {
            return mystring.ToCharArray(startIndex, length);
        }

        public string[] Split(params char[] separator)
        {
            return mystring.Split(separator);
        }

        public string[] Split(char[] separator, int count)
        {
            return mystring.Split(separator, count);
        }

        [ComVisible(false)]
        public string[] Split(char[] separator, StringSplitOptions options)
        {
            return mystring.Split(separator, options);
        }

        [ComVisible(false)]
        public string[] Split(string[] separator, StringSplitOptions options)
        {
            return mystring.Split(separator, options);
        }

        [ComVisible(false)]
        public string[] Split(char[] separator, int count, StringSplitOptions options)
        {
            return mystring.Split(separator, count, options);
        }

        [ComVisible(false)]
        public string[] Split(string[] separator, int count, StringSplitOptions options)
        {
            return mystring.Split(separator, count, options);
        }

        public int IndexOf(char value)
        {
            return mystring.IndexOf(value);
        }

        public int IndexOf(string value)
        {
            return mystring.IndexOf(value);
        }

        public int IndexOf(char value, int startIndex)
        {
            return mystring.IndexOf(value, startIndex);
        }

        public int IndexOf(string value, int startIndex)
        {
            return mystring.IndexOf(value, startIndex);
        }

        public int IndexOf(string value, StringComparison comparisonType)
        {
            return mystring.IndexOf(value, comparisonType);
        }

        public int IndexOf(char value, int startIndex, int count)
        {
            return mystring.IndexOf(value, startIndex, count);
        }

        public int IndexOf(string value, int startIndex, int count)
        {
            return mystring.IndexOf(value, startIndex, count);
        }

        public int IndexOf(string value, int startIndex, StringComparison comparisonType)
        {
            return mystring.IndexOf(value, startIndex, comparisonType);
        }

        public int IndexOf(string value, int startIndex, int count, StringComparison comparisonType)
        {
            return mystring.IndexOf(value, startIndex, count, comparisonType);
        }

        public int LastIndexOf(char value)
        {
            return mystring.LastIndexOf(value);
        }

        public int LastIndexOf(string value)
        {
            return mystring.LastIndexOf(value);
        }

        public int LastIndexOf(char value, int startIndex)
        {
            return mystring.LastIndexOf(value, startIndex);
        }

        public int LastIndexOf(string value, int startIndex)
        {
            return mystring.LastIndexOf(value, startIndex);
        }

        public int LastIndexOf(string value, StringComparison comparisonType)
        {
            return mystring.LastIndexOf(value, comparisonType);
        }

        public int LastIndexOf(char value, int startIndex, int count)
        {
            return mystring.LastIndexOf(value, startIndex, count);
        }

        public int LastIndexOf(string value, int startIndex, int count)
        {
            return mystring.LastIndexOf(value, startIndex, count);
        }

        public int LastIndexOf(string value, int startIndex, StringComparison comparisonType)
        {
            return mystring.LastIndexOf(value, startIndex, comparisonType);
        }

        public int LastIndexOf(string value, int startIndex, int count, StringComparison comparisonType)
        {
            return mystring.LastIndexOf(value, startIndex, count, comparisonType);
        }

        public string Normalize()
        {
            return mystring.Normalize();
        }

        public string Normalize(NormalizationForm normalizationForm)
        {
            return mystring.Normalize(normalizationForm);
        }

        public bool IsNormalized()
        {
            return mystring.IsNormalized();
        }

        public bool IsNormalized(NormalizationForm normalizationForm)
        {
            return mystring.IsNormalized(normalizationForm);
        }

        public string Substring(int startIndex)
        {
            return mystring.Substring(startIndex);
        }

        public string Substring(int startIndex, int length)
        {
            return mystring.Substring(startIndex, length);
        }

        public bool StartsWith(string value)
        {
            return mystring.StartsWith(value);
        }

        [ComVisible(false)]
        public bool StartsWith(string value, StringComparison comparisonType)
        {
            return mystring.StartsWith(value, comparisonType);
        }

        public bool StartsWith(string value, bool ignoreCase, CultureInfo culture)
        {
            return mystring.StartsWith(value, ignoreCase, culture);
        }

        public string Insert(int startIndex, string value)
        {
            return mystring.Insert(startIndex, value);
        }

        public string ToSingular()
        {
            string text = mystring;
            if (text.Length < 3)
            {
                return text;
            }
            if (text.Substring(text.Length - 3, 3).ToLower() == "ies")
            {
                return text.Substring(0, text.Length - 3) + "y";
            }
            if (text.Substring(text.Length - 1, 1).ToLower() == "s")
            {
                return text.Substring(0, text.Length - 1);
            }
            return text;
        }

        public string ToCamel()
        {
            string s = mystring.TrimStart('_');
            s = RemoveSpaces(ToProperCase(s));
            return $"{char.ToLower(s[0])}{s.Substring(1, s.Length - 1)}";
        }

        public string ToCapit()
        {
            string s = mystring.TrimStart('_');
            return RemoveSpaces(ToProperCase(s));
        }

        public string RemoveFinalChar()
        {
            string text = mystring;
            if (text.Length > 1)
            {
                text = text.Substring(0, text.Length - 1);
            }
            return text;
        }

        private string ToProperCase(string s)
        {
            string result = "";
            if (s.Length > 0)
            {
                if (s.IndexOf(" ") > 0)
                {
                    result = Strings.StrConv(s, VbStrConv.ProperCase, 1033);
                }
                else
                {
                    string text = s.Substring(0, 1).ToUpper(new CultureInfo("en-US"));
                    result = text + s.Substring(1, s.Length - 1);
                }
            }
            return result;
        }

        private string ToTrimmedProperCase(string s)
        {
            return RemoveSpaces(ToProperCase(s));
        }

        private string RemoveSpaces(string s)
        {
            s = s.Trim();
            s = s.Replace(" ", "");
            return s;
        }
    }

}
